import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ty_smart_watch/page/health/controller/common_controller.dart';
import 'package:ty_smart_watch/utils/Log.dart';
import 'package:ty_smart_watch/utils/pair.dart';

class ChartView extends StatelessWidget {
  final Pair<Color, Color> color;
  final double maxY;

  const ChartView({super.key, required this.color, required this.maxY});

  @override
  Widget build(BuildContext context) {
    final CommonController controller = Get.find();

    return Container(
        margin: const EdgeInsets.only(right: 20),
        height: 180,
        child: Obx(
          () => BarChart(
            BarChartData(
              maxY: 200,
              alignment: BarChartAlignment.spaceEvenly,
              gridData: const FlGridData(show: false),
              borderData: FlBorderData(
                show: true,
                border: const Border(
                  bottom: BorderSide(color: Colors.grey), // 底部边线的样式
                  left: BorderSide(color: Colors.grey), // 左侧边线的样式
                  right: BorderSide(color: Colors.transparent), // 右侧边线的样式
                  top: BorderSide(color: Colors.transparent), // 顶部边线的样式，这里设置为透明来隐藏
                ),
              ),
              barGroups: _getBarData(controller.dataList.value),
              titlesData: FlTitlesData(
                show: true,
                topTitles: const AxisTitles(
                  sideTitles: SideTitles(showTitles: false),
                ),
                rightTitles: const AxisTitles(
                  sideTitles: SideTitles(showTitles: false),
                ),
                bottomTitles: AxisTitles(
                  sideTitles: SideTitles(
                    showTitles: true,
                    reservedSize: 30,
                    getTitlesWidget: getTitles,
                  ),
                ),
                // bottomTitles: AxisTitles(
                //   sideTitles: _bottomTitles,
                // ),
              ),
            ),
          ),
        ));
  }

  Widget getTitles(double value, TitleMeta meta) {
    final CommonController controller = Get.find();
    const style = TextStyle(
      // fontWeight: FontWeight.bold,
      fontSize: 12,
    );
    String text;
    int length = controller.dataList.value.length;
    if (length == 7) {
      switch (value.toInt()) {
        case 0:
          text = '日';
          break;
        case 1:
          text = '一';
          break;
        case 2:
          text = '二';
          break;
        case 3:
          text = '三';
          break;
        case 4:
          text = '四';
          break;
        case 5:
          text = '五';
          break;
        case 6:
          text = '六';
          break;
        default:
          text = '';
          break;
      }
    } else if (length == 24) {
      switch (value.toInt()) {
        case 0:
          text = '00:00';
          break;
        case 4:
          text = '04:00';
          break;
        case 8:
          text = '08:00';
          break;
        case 12:
          text = '12:00';
          break;
        case 16:
          text = '16:00';
          break;
        case 20:
          text = '20:00';
          break;
        default:
          text = '';
          break;
      }
    } else {
      switch (value.toInt()) {
        case 0:
        case 5:
        case 10:
        case 15:
        case 20:
        case 25:
          text = '${value.toInt() + 1}';
        default:
          if (value.toInt() == length - 1) {
            text = '${value.toInt() + 1}';
          } else {
            text = '';
          }
          break;
      }
    }

    return SideTitleWidget(
      axisSide: meta.axisSide,
      space: 4,
      child: Text(text, style: style),
    );
  }

  List<BarChartGroupData> _getBarData(List<double> list) {
    List<BarChartGroupData> resultList = [];
    for (int i = 0; i < list.length; i++) {
      var res = BarChartGroupData(
        x: i,
        barRods: [
          BarChartRodData(
              toY: list[i],
              color: color.first,
              backDrawRodData: BackgroundBarChartRodData(color: color.second, show: true, toY: 200),
              borderRadius: BorderRadius.zero),
          //   BarChartRodData(
          //       toY: list[i] * 2,
          //       color: Colors.yellow,
          //       backDrawRodData: BackgroundBarChartRodData(color: color.second, show: true, toY: 200),
          //       borderRadius: BorderRadius.zero),
        ],
      );
      resultList.add(res);
    }
    return resultList;
  }
}
